<?php

namespace app\aiv110\controller;

use app\aiv110\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\aiv110\model\Overgroup;
use app\aiv110\model\OvergroupItem;
use app\aiv110\model\CtmOvergroup;
use app\aiv110\model\Custom;
use app\aiv110\model\XcxAudit;
use app\aiv110\model\StoreSoldoutitem;

class OvergroupController extends CommonController {
    /*
     * code列表
     */

    public function code() {
        /*
         * 0  成功
         * 1  参数错误
         * 2  验证失败
         * 10000  您还没有登录
         * 10001  每个超值组合每人限购一次
         * 10002  超值组合已下架
         * 10003  每完成1次活动项目服务，可上传1次照片
         * 10004  每个账号每天点赞同一人只能点赞1次
         */
    }

    /*
     * 超值组合列表
     * author xfwed 
     * date 2019.09.23
     * 
     * @param int perPage 每页数量 非必须，默认20
     * @param int page 当前页数 非必须，默认0
     */

    public function overGroupList() {
        $rule = [
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:0',
        ];
        $msg = [
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '页数为数字',
            'page.egt' => '页数最小为0',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        //0.普通超值组合1.美修斯
        $type = isset($all['type']) ? $all['type'] : 0;
        $store_id = input('param.store_id');
        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }
        $version = input('param.version');
        if ($version) {
            $xcxauditRow = XcxAudit::where('version', $version)->where('is_open', 1)->find();
            if ($xcxauditRow) {
                $where['id'] = ['not in', $xcxauditRow['overgroup_ids']];
            }
        }

        $offset = ($page - 1) * $perPage;
        $where['status'] = Overgroup::STATUS_1;
        if ($store_id) {
            $overgroup_ids = StoreSoldoutitem::where('store_id', $store_id)->value('overgroup_ids');
            if ($overgroup_ids) {
                $where['id'] = ['not in', $overgroup_ids];
            }
        }

        $list = Overgroup::where($where)->where('type', $type)
                ->field('id,name,price,full_price,img,index_img,add_time,type,special_money,sell_count+sell_count_base sell_count')
                ->order('id asc')
                ->limit($offset, $perPage)
                ->select();
        foreach ($list as $k => $v) {
            $itemNum = OvergroupItem::where('overgroup_id', $v['id'])->count();
            //查询包含的项目名称
            $list[$k]['item_name'] = OvergroupItem::alias('o')
                    ->join('item i', 'i.id = o.item_id', 'left')
                    ->where('o.overgroup_id', $v['id'])
                    ->column('item_name');


            $list[$k]['itemNum'] = $itemNum;
//            $list[$k]['special_price'] = 9900 * $itemNum * 0.85;
//            $list[$k]['omit_special_price'] = 9900 * $itemNum * 0.15;

            $list[$k]['special_price'] = $list[$k]['special_money'];
            $list[$k]['omit_special_price'] = $list[$k]['price'] - $list[$k]['special_money'];
        }

        return Utils::ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 超值组合详情
     * author xfwed 
     * date 2019.09.23
     * 
     * @param int perPage 每页数量 非必须，默认20
     * @param int page 当前页数 非必须，默认0
     */

    public function overGroupInfo() {
        $rule = [
            'id' => 'require|integer',
        ];
        $msg = [
            'id.require' => '超值组合id必须',
            'id.integer' => '超值组合id为数字',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return Utils::ajaxReturn(1, $error, null);
        }
        $ctm_id = input('ctm_id');
        $where['id'] = $all['id'];
        $row = Overgroup::with('OvergroupItem.Item')->where($where)->find();
        $row['sell_count'] = $row['sell_count'] + $row['sell_count_base'];
        $itemNum = count($row['overgroup_item']);
        $row['itemNum'] = OvergroupItem::where('overgroup_id', $all['id'])->count();
//        $row['special_price'] = 9900 * $itemNum * 0.85;
//        $row['omit_special_price'] = 9900 * $itemNum * 0.15;

        $og = Overgroup::where('id', '=', $all['id'])
                ->find();

        if ($og['status'] != 1) {
            return Utils::ajaxReturn(1, '项目已下架', null);
        }
        $row['special_price'] = $og['special_money'];
        $row['omit_special_price'] = $og['price'] - $og['special_money'];

        if ($ctm_id) {//查询用户是否购买了该超值组合
            $ctmovergroupRow = CtmOvergroup::where('overgroup_id', '=', $all['id'])
                    ->where('ctm_id', '=', $ctm_id)
                    ->find();
            if ($ctmovergroupRow) {
                $row['is_buy'] = 2; //不限制了  可以买多次
            } else {
                $row['is_buy'] = 2; //1是2否 购买
            }
        } else {
            $row['is_buy'] = 2;
        }
        //查询超值组合的赠品
        $goodsList = Db::name('overgroup_goods')
                ->alias('o')
                ->join(['goods_info'=>'g'],'g.id = o.goods_id','left')
                ->join(['goods_info_detail'=>'d'],'d.goodsId = g.id','left')
                ->where('o.overgroup_id',$all['id'])
                ->field('g.id,g.name,g.icon,d.oriPrice,d.price,o.number,o.note')
                ->group('g.id')
                ->select();
        $row['goodsList'] = $goodsList;
        return Utils::ajaxReturn(0, '查询成功', $row);
    }

    /*
     * 超值组合结算
     * 作者 xfwed
     * 日期 2019-09-23
     *
     * @param int ctm_id 登录的客户编号
     * @param int pro_id 体验卡编号
     */

    public function buyOverGroup() {
        $rule = [
            'ctm_id' => 'require|integer',
            'pro_id' => 'require|integer',
        ];
        $msg = [
            'ctm_id.require' => '用户编号必须',
            'ctm_id.integer' => '用户编号为数字',
            'pro_id.require' => '超值组合编号必须',
            'pro_id.integer' => '超值组合编号为数字',
        ];

        $all = input('post.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $ctm_id = $all['ctm_id'];
        $pro_id = $all['pro_id'];

        if ($ctm_id == 0) {
            return Utils::ajaxReturn(10000, '您还没有登录', null);
        }
//        $ctmOverGroupRow = CtmOvergroup::where('ctm_id', '=', $ctm_id)->where('overgroup_id', $pro_id)->find();
//        if (!is_null($ctmOverGroupRow)) {
//            return Utils::ajaxReturn(10001, '每个超值组合每人限购一次', null);
//        }

        $row = Overgroup::where('id', $pro_id)->field('id,name,price,status,img')->find();
        if ($row['status'] == 2) {
            return Utils::ajaxReturn(10002, '超值组合已下架', null);
        }
        $model = $this->getModelData();
        $ImgPath = config('appconfig.ImgPath');
        //$row['img'] = $ImgPath . $model . '/kkmai/default/overgroup.png';
        //查询用户的零钱
        $ctmData = Custom::where('id', $ctm_id)->field('real_money,red_money,vip_pwd,vip_status')->find();
        //$row['ctmMoney'] = $ctmData['real_money'] + $ctmData['red_money'];
        $row['ctmMoney'] = $ctmData['real_money'];
        if ($ctmData['vip_pwd']) {
            $is_pwd = 1;
        } else {
            $is_pwd = 2;
        }
        $row['is_pwd'] = $is_pwd;

        $itemNum = OvergroupItem::where('overgroup_id', $pro_id)->count();
        //$row['special_price'] = 9900 * $itemNum * 0.85;
//        if($pro_id == 9){
//            $row['special_price'] = 59900;
//        }
        $og = Overgroup::where('id', '=', $pro_id)
                ->find();
        $row['special_price'] = $og['special_money'];
        $row['vip_status'] = $ctmData['vip_status'];
        return Utils::ajaxReturn(0, '查询成功', $row);
    }
    
    /*
     * 招募体验馆
     * author xfwed 
     * date 2021.06.23
     * 
     * @param int perPage 每页数量 非必须，默认20
     * @param int page 当前页数 非必须，默认0
     */

    public function overGroupActivity() {
        $rule = [
            'ctm_id' => 'require|integer',
        ];
        $msg = [
            'ctm_id.require' => '用户编号必须',
            'ctm_id.integer' => '用户编号为数字',
        ];

        $all = input('post.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }
        
        $ctm_id = $all['ctm_id'];
        //查询超值组合的id
        $ids = Db::table('sys_dictionary')
                ->where('type','MOJING_OVERGROUP_IDS')
                ->value('view');
        $idsArray = explode(',', $ids);
        //查询秒杀的id
        $miaoshaitemids = Db::table('sys_dictionary')
                ->where('type','MOJING_MIAOSHA_IDS')
                ->value('view');
        $miaoshaids = Db::name('miaosha')
                ->where('item_id','in',$miaoshaitemids)
                ->where('is_del',1)
                ->column('id');
        
        //背景图片
        $data['bg'][0] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/bg1.png';
        //轮播
        $data['banner']['img'][0] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/bannerImg1.png';
        $data['banner']['img'][1] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/bannerImg2.png';
        $data['banner']['img'][2] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/bannerImg3.png';
        $data['banner']['img'][3] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/bannerImg4.png';
        $data['banner']['img'][4] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/bannerImg5.png';
        $data['banner']['bg'][0] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/bannerbg1.png';
        $data['banner']['bg'][1] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/bannerbg2.png';
        $data['banner']['note'] = '专业AI护肤报告为您精彩呈现';
        //tab
        $data['tab'][0] = '白雪公主试镜';
        $data['tab'][1] = '白雪公主养成记';
        $data['tab'][2] = '谁是最美女人';
        $data['tabImg'] =  'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/tabImg.png';
        //白雪公主试镜
        $data['mode1']['bg'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/mode1bg.png';
        $data['mode1']['item'] = Db::name('miaosha')
                ->alias('m')
                ->join('item i','i.id = m.item_id','left')
                ->where('m.id','in',$miaoshaids)
                ->where('m.is_del',1)
                ->field('m.id,m.price,i.item_price,i.special_money,i.item_img,i.item_name')
                ->select();
        //白雪公主养成记
        $data['mode2']['bg'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/mode2bg.png';
        $OvergroupList = Overgroup::where('id','in',$ids)
                ->field('id,index_img,name,full_price,price,special_money')
                ->select();
        $data['mode2']['item'][0]['id'] = $idsArray[0];
        $data['mode2']['item'][0]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/mode2img1.png';
        $data['mode2']['item'][1]['id'] = $idsArray[1];
        $data['mode2']['item'][1]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/mode2img2.png';
        $data['mode2']['item'][2]['id'] = $idsArray[2];
        $data['mode2']['item'][2]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/mode2img3.png';
        $data['mode2']['item'][3]['id'] = $idsArray[3];
        $data['mode2']['item'][3]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/mode2img4.png';
        //白雪公主生长轨迹
        $orderList = Db::name('order')
                ->alias('o')
                ->join('ord_pro op','op.ord_id = o.id','left')
                ->where('o.type',2)
                ->where('op.pro_id','in',$ids)
                ->where('o.status',2)
                ->where('o.ctm_id',$ctm_id)
                ->field('o.id')
                ->group('o.id')
                ->select();
        $facemode3img1 = Db::name('mojing_face')
                ->where('ctm_id',$ctm_id)
                ->where('places',1)
                ->order('id desc')
                ->value('img');
        if($facemode3img1){
            $mode3img1 = $facemode3img1;
        }  else {
            $mode3img1 = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img1.png';
        }
        
        $facemode3img2 = Db::name('mojing_face')
                ->where('ctm_id',$ctm_id)
                ->where('places',2)
                ->order('id desc')
                ->value('img');
        if($facemode3img2){
            $mode3img2 = $facemode3img2;
        }  else {
            $mode3img2 = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img2.png';
        }
        
        $facemode3img3 = Db::name('mojing_face')
                ->where('ctm_id',$ctm_id)
                ->where('places',3)
                ->order('id desc')
                ->value('img');
        if($facemode3img3){
            $mode3img3 = $facemode3img3;
        }  else {
            $mode3img3 = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img3.png';
        }
        
        $facemode3img4 = Db::name('mojing_face')
                ->where('ctm_id',$ctm_id)
                ->where('places',4)
                ->order('id desc')
                ->value('img');
        if($facemode3img4){
            $mode3img4 = $facemode3img4;
        }  else {
            $mode3img4 = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img4.png';
        }
        
        $facemode3img5 = Db::name('mojing_face')
                ->where('ctm_id',$ctm_id)
                ->where('places',5)
                ->order('id desc')
                ->value('img');
        if($facemode3img5){
            $mode3img5 = $facemode3img5;
        }  else {
            $mode3img5 = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img5.png';
        }
        
        $facemode3img6 = Db::name('mojing_face')
                ->where('ctm_id',$ctm_id)
                ->where('places',6)
                ->order('id desc')
                ->value('img');
        if($facemode3img6){
            $mode3img6 = $facemode3img6;
        }  else {
            $mode3img6 = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img6.png';
        }
        
        $facemode3img7 = Db::name('mojing_face')
                ->where('ctm_id',$ctm_id)
                ->where('places',7)
                ->order('id desc')
                ->value('img');
        if($facemode3img7){
            $mode3img7 = $facemode3img7;
        }  else {
            $mode3img7 = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img7.png';
        }
        foreach ($orderList as $k => $v){
            $order_id = $v['id'];
            $yuyueList = Db::name('yuyue')
                    ->where('order_id',$order_id)
                    ->field('id,status,staff_time')
                    ->orderRaw('staff_time is null asc,staff_time asc')
                    ->select();
//            print_r(Db::name('yuyue')->getLastSql());die;
            foreach ($yuyueList as $key => $val){
                if($key == 0){
                    $yuyueList[$key]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img1.png';
                    $yuyueList[$key]['ctmimg'] = $mode3img1;
                    $yuyueList[$key]['note'] = '试镜成功 公主登场';
                }elseif ($key == 1) {
                    $yuyueList[$key]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img2.png';
                    $yuyueList[$key]['ctmimg'] = $mode3img2;
                    $yuyueList[$key]['note'] = '魔镜箴言 皇后怒颜';
                }elseif ($key == 2) {
                    $yuyueList[$key]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img4.png';
                    $yuyueList[$key]['ctmimg'] = $mode3img4;
                    $yuyueList[$key]['note'] = '护卫怜惜 躲过一劫';
                }elseif ($key == 3) {
                    $yuyueList[$key]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img3.png';
                    $yuyueList[$key]['ctmimg'] = $mode3img3;
                    $yuyueList[$key]['note'] = '幸得矮人 暂得安稳';
                }elseif ($key == 4) {
                    $yuyueList[$key]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img5.png';
                    $yuyueList[$key]['ctmimg'] = $mode3img5;
                    $yuyueList[$key]['note'] = '善良公主 误食毒果';
                }elseif ($key == 5) {
                    $yuyueList[$key]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img6.png';
                    $yuyueList[$key]['ctmimg'] = $mode3img6;
                    $yuyueList[$key]['note'] = '甜蜜一吻 公主苏醒';
                }elseif ($key == 6) {
                    $yuyueList[$key]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img7.png';
                    $yuyueList[$key]['ctmimg'] = $mode3img7;
                    $yuyueList[$key]['note'] = '最美公主 非你莫属';
                }
            }
            $data['mode3'][$k] = $yuyueList;
        }
        if(empty($yuyueList)){
            $yuyueList[0]['id'] = '1';
            $yuyueList[0]['status'] = '1';
            $yuyueList[0]['staff_time'] = '';
            $yuyueList[0]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img1.png';
            $yuyueList[0]['note'] = '试镜成功 公主登场';
            $yuyueList[1]['id'] = '1';
            $yuyueList[1]['status'] = '1';
            $yuyueList[1]['staff_time'] = '';
            $yuyueList[1]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img2.png';
            $yuyueList[1]['note'] = '魔镜箴言 皇后怒颜';
            $yuyueList[2]['id'] = '1';
            $yuyueList[2]['status'] = '1';
            $yuyueList[2]['staff_time'] = '';
            $yuyueList[2]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img4.png';
            $yuyueList[2]['note'] = '护卫怜惜 躲过一劫';
            $yuyueList[3]['id'] = '1';
            $yuyueList[3]['status'] = '1';
            $yuyueList[3]['staff_time'] = '';
            $yuyueList[3]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img3.png';
            $yuyueList[3]['note'] = '幸得矮人 暂得安稳';
            $yuyueList[4]['id'] = '1';
            $yuyueList[4]['status'] = '1';
            $yuyueList[4]['staff_time'] = '';
            $yuyueList[4]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img5.png';
            $yuyueList[4]['note'] = '善良公主 误食毒果';
            $yuyueList[5]['id'] = '1';
            $yuyueList[5]['status'] = '1';
            $yuyueList[5]['staff_time'] = '';
            $yuyueList[5]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img6.png';
            $yuyueList[5]['note'] = '甜蜜一吻 公主苏醒';
            $yuyueList[6]['id'] = '1';
            $yuyueList[6]['status'] = '1';
            $yuyueList[6]['staff_time'] = '';
            $yuyueList[6]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/newmode3img7.png';
            $yuyueList[6]['note'] = '最美公主 非你莫属';
            $data['mode3'][0] = $yuyueList;
        }
        //魔镜魔镜 谁是世界上最美的女人
        $data['mode4']['bg'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/mode4bg.png';
        $data['mode4']['kongbg'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/mode4kongbg.png';
        
        $matSql = Db::name('order')
                ->alias('o')
                ->join('ord_pro op','op.ord_id = o.id','left')
                ->join('custom c','c.id = o.ctm_id','left')
                ->where('o.type',5)
                ->where('op.pro_id','in',$miaoshaids)
                ->where('o.status',2)
                ->field('c.id,c.custom_name,c.custom_photo,c.mojing_score,c.mojing_time')
                ->group('o.ctm_id')
                ->order('c.mojing_time desc')
                ->buildSql();
        $matSql1 = Db::name('order')
                ->alias('o')
                ->join('ord_pro op','op.ord_id = o.id','left')
                ->join('custom c','c.id = o.ctm_id','left')
                ->where('o.type',2)
                ->where('op.pro_id','in',$ids)
                ->where('o.status',2)
                ->field('c.id,c.custom_name,c.custom_photo,c.mojing_score,c.mojing_time')
                ->group('o.ctm_id')
                ->order('mojing_time desc')
                ->buildSql();
        
        //$c = Db::name('order')->field(字段)->where(条件)->union([$a, $b])->buildSql();
        $c = Db::name('order')
                ->alias('o')
                ->join('ord_pro op','op.ord_id = o.id','left')
                ->join('custom c','c.id = o.ctm_id','left')
                ->where('o.type',2)
                ->where('op.pro_id','in',$ids)
                ->where('o.status',2)
                ->field('c.id,c.custom_name,c.custom_photo,c.mojing_score,c.mojing_time')
                ->union([$matSql,$matSql1])
                ->group('o.ctm_id')
                ->order('mojing_time desc')
                ->buildSql();
        $ctmList = Db::table($c . ' a')
                ->order('a.mojing_time desc')
                ->group('a.id')
                ->select();// $c . a是零时表名
//        $ctmList = Db::name('order')
//                ->alias('o')
//                ->join('ord_pro op','op.ord_id = o.id','left')
//                ->join('custom c','c.id = o.ctm_id','left')
//                ->where('o.type',2)
//                ->where('op.pro_id','in',$ids)
//                ->where('o.status',2)
//                ->field('c.id,c.custom_name,c.custom_photo,c.mojing_score,c.mojing_time')
//                ->union($matSql, true)
//                ->group('ctm_id')
//                ->order('mojing_time desc')
//                ->select();
//        print_r(Db::name('order')->getLastSql());
        //当天开始时间
        $start_time=strtotime(date("Y-m-d",time()));
        //当天结束之间
        $end_time=$start_time+60*60*24;
        $start_time=date("Y-m-d H:i:s",$start_time);       
        $end_time=date("Y-m-d H:i:s",$end_time);  
        foreach ($ctmList as $key => $val){
            $ctmId = $val['id'];
            //点赞 图片
            $likeNum = Db::name('mojing_activity')
                    ->where('ctm_id',$ctmId)
                    ->where('type',2)
                    ->count();
            $ctmList[$key]['likeNum'] = $likeNum;
            $img = Db::name('mojing_activity')
                    ->where('ctm_id',$ctmId)
                    ->where('type',1)
                    ->field('img')
                    ->select();
            $ctmList[$key]['img'] = $img;
            $likeRow = Db::name('mojing_activity')
                    ->where('ctm_id',$ctmId)
                    ->where('like_ctm_id',$ctm_id)
                    ->where('type',2)
                    ->where('add_time','between',[$start_time,$end_time])
                    ->find();
            if($likeRow){
                $is_like = 1;//1是2否今日已经点赞
            }  else {
                $is_like = 2;//1是2否今日已经点赞
            }
            $ctmList[$key]['is_like'] = $is_like;
            
        }
        $data['mode4']['ctmList'] = $ctmList;
        //按钮
        $data['button'][0] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/button1.png';
        $data['button'][1] = 'http://47.104.83.107/images/kkmimg/kkmai/default/mojingActivity/button3.png';
        
        return Utils::ajaxReturn(0, '查询成功', $data);
    }
    
    /*
     * 我变美了
     * author xfwed 
     * date 2021.06.30
     */

    public function overGroupActivityUploadImg() {
        $rule = [
            'ctm_id' => 'require|integer',
            'img' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户编号必须',
            'ctm_id.integer' => '用户编号为数字',
            'img.require' => '图片必须',
        ];

        $all = input('post.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $ctm_id = $all['ctm_id'];
        $img = $all['img'];
        $ids = Db::table('sys_dictionary')
                ->where('type','MOJING_OVERGROUP_IDS')
                ->value('view');
        //查询用户做完几次服务
        $serNum = Db::name('yuyue')
                ->alias('y')
                ->join('ord_pro op','op.ord_id = y.order_id','left')
                ->where('op.type',2)
                ->where('op.pro_id','in',$ids)
                ->where('y.status','4,7,8')
                ->where('y.ctm_id',$ctm_id)
                ->count();
        $imgNum = Db::name('mojing_activity')
                ->where('ctm_id',$ctm_id)
                ->where('type',1)
                ->count();
        if($imgNum >= $serNum){
            return Utils::ajaxReturn(10003, '每完成1次活动项目服务，可上传1次照片', null);
        }
        $data['type'] = 1;
        $data['ctm_id'] = $ctm_id;
        $data['img'] = $img;
        $data['add_time'] = date('Y-m-d H:i:s');
        $row = Db::name('mojing_activity')->insert($data);
        if($row){
            return Utils::ajaxReturn(0, '上传成功', null);
        }  else {
            return Utils::ajaxReturn(3, '上传失败', null);
        }
    }
    
    /*
     * 点赞
     * author xfwed 
     * date 2021.06.30
     */

    public function overGroupActivityLike() {
        $rule = [
            'ctm_id' => 'require|integer',
            'like_ctm_id' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '被点赞的用户id必须',
            'ctm_id.integer' => '被点赞的用户id为数字',
            'like_ctm_id.require' => '点赞用户id必须',
        ];

        $all = input('post.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $ctm_id = $all['ctm_id'];//被点赞的用户
        $like_ctm_id = $all['like_ctm_id'];//点赞的用户
        //查询当天用户是否为该用户点过赞
        //当天开始时间
        $start_time=strtotime(date("Y-m-d",time()));
        //当天结束之间
        $end_time=$start_time+60*60*24;
        $start_time=date("Y-m-d H:i:s",$start_time);       
        $end_time=date("Y-m-d H:i:s",$end_time);  
        $likeNum = Db::name('mojing_activity')
                ->where('type',2)
                ->where('ctm_id',$ctm_id)
                ->where('like_ctm_id',$like_ctm_id)
                ->where('add_time','between',[$start_time,$end_time])
                ->find();
        if($likeNum){
            return Utils::ajaxReturn(10004, '今日已点赞，明天再来试试吧~', null);
        }
        $data['type'] = 2;
        $data['ctm_id'] = $ctm_id;
        $data['like_ctm_id'] = $like_ctm_id;
        $data['add_time'] = date('Y-m-d H:i:s');
        $row = Db::name('mojing_activity')->insert($data);
        if($row){
            return Utils::ajaxReturn(0, '点赞成功', null);
        }  else {
            return Utils::ajaxReturn(3, '点赞失败', null);
        }
    }
    
    /*
     * 跑任务更新用户的综合分
     * 每天跑一次
     * author xfwed 
     * date 2021.06.30
     * 
     * 综合分=以魔镜测肤差值（20%）+服务完成次数（50%）+点赞数（30%）；
     * 魔镜测肤综合分差值；
     * 完成1次服务得1分；
     * 每获得1次点赞得1分，每个账号每天点赞同一人只能点赞1次
     */

    public function overGroupActivityCtmScore() {
        $ids = Db::table('sys_dictionary')
                ->where('type','MOJING_OVERGROUP_IDS')
                ->value('view');
        //查询秒杀的id
        $miaoshaitemids = Db::table('sys_dictionary')
                ->where('type','MOJING_MIAOSHA_IDS')
                ->value('view');
        $miaoshaids = Db::name('miaosha')
                ->where('item_id','in',$miaoshaitemids)
                ->where('is_del',1)
                ->column('id');
//        $ctmList = Db::name('yuyue')
//                ->alias('y')
//                ->join('ord_pro op','op.ord_id = y.order_id','left')
//                ->where('op.type',2)
//                ->where('op.pro_id','in',$ids)
//                ->field('y.ctm_id')
//                ->group('y.ctm_id')
//                ->select();
        
        $matSql = Db::name('order')
                ->alias('o')
                ->join('ord_pro op','op.ord_id = o.id','left')
                ->join('custom c','c.id = o.ctm_id','left')
                ->where('o.type',5)
                ->where('op.pro_id','in',$miaoshaids)
                ->where('o.status',2)
                ->field('c.id,c.custom_name,c.custom_photo,c.mojing_score,c.mojing_time')
                ->group('o.ctm_id')
                ->order('c.mojing_time desc')
                ->buildSql();
        $matSql1 = Db::name('order')
                ->alias('o')
                ->join('ord_pro op','op.ord_id = o.id','left')
                ->join('custom c','c.id = o.ctm_id','left')
                ->where('o.type',2)
                ->where('op.pro_id','in',$ids)
                ->where('o.status',2)
                ->field('c.id,c.custom_name,c.custom_photo,c.mojing_score,c.mojing_time')
                ->group('o.ctm_id')
                ->order('mojing_time desc')
                ->buildSql();
        
        //$c = Db::name('order')->field(字段)->where(条件)->union([$a, $b])->buildSql();
        $c = Db::name('order')
                ->alias('o')
                ->join('ord_pro op','op.ord_id = o.id','left')
                ->join('custom c','c.id = o.ctm_id','left')
                ->where('o.type',2)
                ->where('op.pro_id','in',$ids)
                ->where('o.status',2)
                ->field('c.id,c.custom_name,c.custom_photo,c.mojing_score,c.mojing_time')
                ->union([$matSql,$matSql1])
                ->group('o.ctm_id')
                ->order('mojing_time desc')
                ->buildSql();
        $ctmList = Db::table($c . ' a')
                ->order('a.mojing_time desc')
                ->group('a.id')
                ->select();// $c . a是零时表名
        
        foreach ($ctmList as $k => $v){
            $ctm_id = $v['id'];
            $serNum = Db::name('yuyue')
                ->alias('y')
                ->join('ord_pro op','op.ord_id = y.order_id','left')
                ->where('op.type',2)
                ->where('op.pro_id','in',$ids)
                ->where('y.status','4,7,8')
                ->where('y.ctm_id',$ctm_id)
                ->count();
            $likeNum = Db::name('mojing_activity')
                ->where('type',2)
                ->where('ctm_id',$ctm_id)
                ->count();
            $score = $serNum*0.5 + $likeNum*0.3;
            //更新时间  有点赞、上传照片、完成服务、购买项目，都算更新；
            $ctmRow = Db::name('custom')
                    ->where('id',$ctm_id)
                    ->field('id,mojing_time')
                    ->find();
            $old_mojing_time = $ctmRow['mojing_time'];
            $mojing_time = $old_mojing_time;
            $zan_time = Db::name('mojing_activity')
                ->where('ctm_id',$ctm_id)
                ->order('add_time desc')    
                ->find();
            if($zan_time){
                if($zan_time['add_time'] > $old_mojing_time){
                    $mojing_time = $zan_time['add_time'];
                }
            }
            //完成服务的时间
            $o_staff_time = Db::name('yuyue')
                ->alias('y')
                ->join('ord_pro op','op.ord_id = y.order_id','left')
                ->where('op.type',2)
                ->where('op.pro_id','in',$ids)
                ->where('y.ctm_id',$ctm_id)
                ->field('y.staff_time')
                ->order('y.staff_time desc')
                ->find();
            $m_staff_time = Db::name('yuyue')
                ->alias('y')
                ->join('ord_pro op','op.ord_id = y.order_id','left')
                ->where('op.type',5)
                ->where('op.pro_id','in',$miaoshaids)
                ->where('y.ctm_id',$ctm_id)
                ->field('y.staff_time')
                ->order('y.staff_time desc')
                ->find();
            if($o_staff_time['staff_time'] > $m_staff_time['staff_time']){
                if($mojing_time < $o_staff_time['staff_time']){
                    $mojing_time = $o_staff_time['staff_time'];
                }
            }  else {
                if($mojing_time < $m_staff_time['staff_time']){
                    $mojing_time = $m_staff_time['staff_time'];
                }
            }
            Db::name('custom')
                    ->where('id',$ctm_id)
                    ->update(['mojing_score'=>$score,'mojing_time'=>$mojing_time]);
        }
        
    }
    
     /*
     * 记录用户换脸图片
     * author xfwed 
     * date 2021.07.06
     */

    public function addMojingFace() {
        $rule = [
            'ctm_id' => 'require|integer',
            'places' => 'require',
            'img' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户id必须',
            'ctm_id.integer' => '用户id为数字',
            'places.require' => '第几张图片必须',
            'img.require' => '图片路径必须',
        ];

        $all = input('post.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $ctm_id = $all['ctm_id'];//用户
        $places = $all['places'];//第几张图片
        $img = $all['img'];//图片路径
        
        $data['ctm_id'] = $ctm_id;
        $data['places'] = $places;
        $data['img'] = $img;
        $data['add_time'] = date('Y-m-d H:i:s');
        $row = Db::name('mojing_face')->insert($data);
        if($row){
            return Utils::ajaxReturn(0, '成功', null);
        }  else {
            return Utils::ajaxReturn(3, '失败', null);
        }
    }
    
    /*
     * 用户AI报告
     * author xfwed 
     * date 2021.07.06
     */

    public function mojingReport() {
        $rule = [
            'ctm_id' => 'require|integer',
            'staff_time' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户id必须',
            'ctm_id.integer' => '用户id为数字',
            'staff_time.require' => '时间必须',
        ];

        $all = input('post.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $ctm_id = $all['ctm_id'];//用户
        $staff_time = $all['staff_time'];//时间 查这个用户在这个时间的当天分数最高的一次测试
        $staff_time = strtotime($staff_time);
        $start_time = strtotime(date("Y-m-d",$staff_time));
        $end_time = $start_time+60*60*24;
        $start_time = date('Y-m-d H:i:s',$start_time);
        $end_time = date('Y-m-d H:i:s',$end_time);
        $row = Db::name('het_data')
                ->where('ctm_id',$ctm_id)
                ->where('add_time','between',[$start_time,$end_time])
                ->order('score desc')
                ->field('id,h5url,score,add_time')
                ->find();
        if($row){
            return Utils::ajaxReturn(0, '查询成功', $row);
        }  else {
            return Utils::ajaxReturn(3, '失败', null);
        }
    }
    

}
